package com.study.talk.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;

import androidx.core.os.EnvironmentCompat;

import com.study.talk.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtil {
    public static File uriToFile(Uri uri, Context context,String fileName) throws IOException {
        ContentResolver contentResolver = context.getContentResolver();
        File file = createFile(context, fileName);
        try(InputStream inputStream = contentResolver.openInputStream(uri);
            OutputStream outputStream = new FileOutputStream(file)){
                if (inputStream != null) {
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                    }
            }
        }
        return file;
    }


    public static File createFile(Context context,String fileName) throws IOException {
        File file = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        if (file == null){
            ToastUtil.showToast(context,"External storage directory not available");
            throw new IOException("External storage directory not available");
        }else {
            File result = new File(file, fileName);
            if (result.exists()){
                result.delete();
            }
            return result;
        }
    }

    public static String getPathFromUri(Context context,Uri uri){
        String path = "";
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null && cursor.moveToFirst()){
            int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            path = cursor.getString(index);
            cursor.close();
        }

        if (TextUtils.isEmpty(path)){
            path = getFilePathForNonMediaUri(context,uri);
        }
        return path;
    }
    public static String getFilePathForNonMediaUri(Context context, Uri uri) {
        String filePath = "";
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndexOrThrow("_data");
                filePath = cursor.getString(columnIndex);
            }
            cursor.close();
        }
        return filePath;
    }

    public static Uri getFallbackRingtoneUri(Context context) {
        final String packageName = context.getPackageName();
        return Uri.parse("android.resource://" + packageName + "/" + R.drawable.choose_image);
    }

}
